//
//  DYDragCellCollectionView.m
//  dyss_v2
//
//  Created by LXF on 16/1/27.
//  Copyright © 2016年 First Facility. All rights reserved.
//

#import "DYDragCellCollectionView.h"
#import <CustomLogTools.h>
#import <UIKit_Category.h>

@interface DYDragCellCollectionView (){
    BOOL _editing;
}

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;

@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *finishButton;

@end

@implementation DYDragCellCollectionView

-(void)shakeAllCellIfNeed{
    if (self.editing) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self xwp_shakeAllCell];
        });
    }
}

- (void)cancelButton_onClick{
    printD(@"单击摁钮");
    self.editing = NO;
}
- (void)finishButton_onClick{
    printD(@"单击摁钮");
    self.editing = NO;
}

-(BOOL)editing{
    return _editing;
}

-(void)setEditing:(BOOL)editing{
    if (_editing == editing) {
        return;
    }
    _editing = editing;
    if (editing) {
        [self xwp_shakeAllCell];
        [self.window addSubview:self.headerView];
        [self.window addSubview:self.footerView];
        self.headerView.alpha = 0;
        self.footerView.alpha = 0;
    }else{
        [self xwp_stopShakeAllCell];
        self.headerView.alpha = 1;
        self.footerView.alpha = 1;
    }
    [UIView animateWithDuration:0.2 animations:^{
        if (editing) {
            self.headerView.alpha = 1;
            self.footerView.alpha = 1;
        }else{
            self.headerView.alpha = 0;
            self.footerView.alpha = 0;
        }
    }completion:^(BOOL finished) {
        if (editing == false) {
            [self.headerView removeFromSuperview];
            [self.footerView removeFromSuperview];
        }
    }];
    
    if([self.delegate respondsToSelector:@selector(dragCellCollectionView:editingChangTo:)]){
        [(id<DYDragCellCollectionViewDelegate>)self.delegate dragCellCollectionView:self editingChangTo:editing];
    }
    
    if(editing == NO && [self.delegate respondsToSelector:@selector(dragCellCollectionViewEndEditing:)]){
        [(id<DYDragCellCollectionViewDelegate>)self.delegate dragCellCollectionViewEndEditing:self];
    }
    
}

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    if (self.editing) {
//        self.editing = NO;
//        return;
//    }
//    [super touchesBegan:touches withEvent:event];
//}

- (void)xwp_gestureBegan2:(UILongPressGestureRecognizer *)longPressGesture{
    self.editing = YES;
}
- (void)xwp_gestureBegan3:(UILongPressGestureRecognizer *)longPressGesture{
    self.editing = NO;
}

-(UIView *)headerView{
    if (_headerView == nil) {
        UIView *a = [[UIView alloc] init];
        a.backgroundColor = [UIColor whiteColor];
        a.frame = self.window.frame;
        a.height = 64;
        
        UILabel *label = [[UILabel alloc]init];
        label.text = @"编辑模式";
        [label sizeToFit];
        
        [a addSubview:label];
        [label xx_AlignInnerWithType:xx_AlignTypeCenterCenter referView:a size:CGSizeNull() offset:CGPointZero];

        _headerView = a;
    }
    return _headerView;
}
-(UIView *)footerView{
    if (_footerView == nil) {
        UIView *a = [[UIView alloc] init];
        a.backgroundColor = [UIColor redColor];
        a.frame = self.window.frame;
        a.y = self.window.height - 49;
        a.height = 49;
        
//        [a addSubview:self.cancelButton];
        [a addSubview:self.finishButton];
//        [a xx_HorizontalTileWithSubviews:@[self.cancelButton,self.finishButton] insets:UIEdgeInsetsZero];
        
        [self.finishButton xx_FillWithReferView:a insets:UIEdgeInsetsZero];
        
        _footerView = a;
    }
    return _footerView;
}

-(UIButton *)cancelButton{
    if (_cancelButton == nil) {
        UIButton *a = [UIButton buttonWithType:UIButtonTypeSystem];
        [a setTitle:@"取消" forState:UIControlStateNormal];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [a setBackgroundColor:[UIColor whiteColor]];
        [a addTarget:self action:@selector(cancelButton_onClick) forControlEvents:UIControlEventTouchUpInside];
        _cancelButton = a;
    }
    return _cancelButton;
}
-(UIButton *)finishButton{
    if (_finishButton == nil) {
        UIButton *a = [UIButton buttonWithType:UIButtonTypeSystem];
        [a setTitle:NSLocalizedString(@"完成", @"") forState:UIControlStateNormal];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [a setBackgroundColor:[UIColor whiteColor]];
        [a addTarget:self action:@selector(finishButton_onClick) forControlEvents:UIControlEventTouchUpInside];
        _finishButton = a;
    }
    return _finishButton;
}


@end
